﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Drawing.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

using DotSpatial.Geometries;
using DotSpatial.Data;

namespace HuanGis
{
   public  class FDGridLayer:FELineLayer 
    {

       public FDGridLayer(string id)
           : base(id)
       {

           this.m_bShapeEditable = false;




           this._XCells = 10;
           this._YCells = 10;
           this._XUnit = 1;
           this._YUnit = 1;
           this._LeftBottomX = 0;
           this._LeftBottomY = 0;

           this._OffsetX = 0;
           this._OffsetY = 0;


           //标记其为不可选择
           this.BSelectable = false;

 

       }

       //需要标明的属性包括 偏移量，标注，坐标轴，左下角坐标值，网格数等得呢个

       # region 字段

       private int _XCells = 10;
       private int _YCells = 10;
       private double _LeftBottomX = 0;
       private double _LeftBottomY = 0;
       private double _XUnit = 1;
       private double _YUnit = 1;

       private bool _IsShowXLine = true;
       private bool _IsShowYLine = true;

       private double _OffsetX = 0;
       private double _OffsetY = 0;

       # endregion

       # region 属性

       /// <summary>
       /// X轴单元格数")]
       /// </summary>
       [Category ("特定设置"),DisplayName ("X轴单元格数")]
       public int XCells
       {
           get { return this._XCells; }
           set
           {
               if (value < 0)
               {
                   value = 0;
                }
               this._XCells = value;
               if (this.BRunning)
                   this.Redraw();
           }
       
       }

       /// <summary>
       /// Y轴单元格数
       /// </summary>
       [Category ("特定设置"),DisplayName ("Y轴单元格数")]
       public int YCells
       {
           get { return this._YCells; }
           set
           {
               if (value < 0)
               { 
                   value = 0; 
               }
               this._YCells = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// X轴单元格长度
       /// </summary>
       [Category ("特定设置"),DisplayName ("X轴单元格长度")]
       public double XUnit
       {
           get { return this._XUnit; }
           set
           {
               if (value < 0)
                   value = 0;
               this._XUnit = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// Y轴单元格长度
       /// </summary>
       [Category("特定设置"), DisplayName("Y轴单元格长度")]
       public double YUnit
       {
           get { return this._YUnit; }
           set
           {
               if (value < 0)
                   value = 0;
               this._YUnit = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 左下角点绝对坐标X
       /// </summary>
       [Category("特定设置"), Browsable(false), DisplayName("左下角点绝对坐标X")]
       public double LeftBottomX
       {
           get { return this._LeftBottomX; }
           set
           {
               this._LeftBottomX = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }


       /// <summary>
       /// 左下角点绝对坐标Y
       /// </summary>
       [Category("特定设置"), Browsable(false), DisplayName("左下角点绝对坐标Y")]
       public double LeftBottomY
       {
           get { return this._LeftBottomY; }
           set
           {
               this._LeftBottomY = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 显示横向网格线
       /// </summary>
       [Category ("特定设置"),DisplayName ("显示横向网格线")]
       public bool IsShowXLine
       {
           get { return this._IsShowXLine; }
           set 
           {
               this._IsShowXLine = value;
               if (this.BRunning)
                   this.Redraw();
           }
       
       }

       /// <summary>
       /// 显示纵向网格线
       /// </summary>
       [Category ("特定设置"),DisplayName ("显示纵向网格线")]
       public bool IsShowYLine
       {
           get { return this._IsShowYLine; }
           set 
           {
               this._IsShowYLine = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 相对坐标相对于绝对坐标在X方向的偏移量
       /// </summary>
       [Browsable (false)]
       public double OffsetX
       {
           get { return this._OffsetX; }
           set 
           {
               this._OffsetX = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       /// <summary>
       /// 相对坐标相对于绝对坐标在Y方向的偏移量
       /// </summary>
       [Browsable (false )]
       public double OffsetY
       {
           get { return this._OffsetY; }
           set 
           {
               this._OffsetY = value;
               if (this.BRunning)
                   this.Redraw();
           }
       }

       # endregion


       /// <summary>
       /// 用于构建网格的线条feature
       /// </summary>
       protected void BuildGridLine()
       {
 
       }

       protected void BuildAxix()
       {
 
       }


       protected void DrawGridLine()
       {           
           
           //所有横线左侧X坐标
           double LeftX = this._LeftBottomX + this._OffsetX;
           //所有横线右侧X坐标
           double RightX = this._LeftBottomX + this._OffsetX + this._XUnit * this._XCells;
           //所有纵线底部Y坐标
           double BottomY = this._LeftBottomY + this._OffsetY;
           //所有纵线顶部Y坐标
           double TopY = this._LeftBottomY + this._OffsetY + this._YUnit * this._YCells;

           //定义一个Shape足够了吧！！！！！！！！
           Shape shp = new Shape(FeatureTypes.Line);
           
           
           //有_YCells个网格，所以有_YCells+1个横线
           for (int i = 0; i < this._YCells + 1; i++)
           {
               Coordinate[] c = new Coordinate[2];
               c[0] = new Coordinate(LeftX, BottomY + this._YUnit * i);
               c[1] = new Coordinate(RightX, BottomY + this._YUnit * i);
                              
               shp.AddPart(c, CoordinateTypes.Regular);                             
           }


           //有_XCells个网格，所以有_XCells+1个纵线
           for (int i = 0; i < this._XCells + 1; i++)
           {
               Coordinate[] c = new Coordinate[2];
               c[0] = new Coordinate(LeftX + this._XUnit * i, BottomY);
               c[1] = new Coordinate(LeftX + this._XUnit * i, TopY);

               shp.AddPart(c, CoordinateTypes.Regular);
           }

           //一个Feature也足够了
           Feature f = new Feature(shp);
           if (f != null)
               (this.ILayer.DataSet as IFeatureSet).AddFeature(f);
       }

       protected void DrawAxis()
       { 
       }

      protected void  DrawAxisText()
      {

      }

      public void SetGridProperty(int XCells, int YCells, double LeftBottomX, double LeftBottomY,
          double XUnit, double YUnit, double OffsetX, double OffsetY)
      {
          this._XCells = XCells;
          this._YCells = YCells;
          this._LeftBottomX = LeftBottomX;
          this._LeftBottomY = LeftBottomY;
          this._XUnit = XUnit;
          this._YUnit = YUnit;
          this._OffsetX = OffsetX;
          this._OffsetY = OffsetY;

          if (this.BRunning)
              this.Redraw();
      }


      protected  override void BuildLayer()
      {
          base.BuildLayer();


         

      }

       //根据属性进行重新绘制
       protected  override void Redraw()
       {
           (this.ILayer.DataSet as IFeatureSet).Features.Clear(); 

           this.DrawGridLine();
           this.DrawAxis();
           this.DrawAxisText();

           (this.ILayer.DataSet as IFeatureSet).UpdateEnvelopes();
           (this.ILayer.DataSet as IFeatureSet).InvalidateVertices();



       }

    }
}
